package com.cuit.mimall.controller.backend;

import com.cuit.mimall.common.R;
import com.cuit.mimall.service.ISeckillProductService;
import com.cuit.mimall.service.ISeckillTimeService;
import com.cuit.mimall.util.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author : TeaWu
 */
@RestController("backendSeckillController")
@RequestMapping("/api/v1/backend/seckill")
public class seckillController {
    private final RedisCache redisCache;

    private final ISeckillProductService seckillProductService;

    private final ISeckillTimeService seckillTimeService;

    @Autowired
    public seckillController(RedisCache redisCache, ISeckillProductService seckillProductService, ISeckillTimeService seckillTimeService) {
        this.redisCache = redisCache;
        this.seckillProductService = seckillProductService;
        this.seckillTimeService = seckillTimeService;
    }

    /**
     * 获取秒杀时间段
     *
     * @return R
     */
    @GetMapping("/time")
    public R<?> getSeckillTime() {
        return R.ok(seckillTimeService.getSeckillTime());
    }

    /**
     * 开始秒杀
     *
     * @param seckillId 秒杀商品id
     * @return R
     */
    @PostMapping("/seckill/{seckillId}")
    public R<?> seckillProduct(@PathVariable String seckillId, @CookieValue("XM_TOKEN") String cookie) {
        // 获取当前用户id
        Integer userId = Integer.valueOf(redisCache.getCacheObject(cookie));
        seckillProductService.seckillProduct(seckillId, userId);
        return R.ok("排队中");
    }
}
